package cn.edu.cjw.APMFP.ui.panel;

import cn.edu.cjw.APMFP.BOMTable.pojo.BOMTable;
import cn.edu.cjw.APMFP.Product.Servicve.ProductService;
import cn.edu.cjw.APMFP.Product.Servicve.ProductServiceImpl;
import cn.edu.cjw.APMFP.Product.pojo.Product;
import cn.edu.cjw.APMFP.ProductionTask.Service.TaskService;
import cn.edu.cjw.APMFP.ProductionTask.Service.TaskServiceImpl;
import cn.edu.cjw.APMFP.ProductionTask.pojo.ProductionTask;
import cn.edu.cjw.APMFP.Util.StringUtil;
import cn.edu.cjw.APMFP.WorkStep.Service.WsService;
import cn.edu.cjw.APMFP.WorkStep.Service.WsServiceImpl;
import cn.edu.cjw.APMFP.WorkStep.pojo.WorkStep;
import cn.edu.cjw.APMFP.ui.CenterPanel;
import cn.edu.cjw.APMFP.ui.component.combobox.JFilterComboBox;
import cn.edu.cjw.APMFP.ui.component.combobox.MultiComboBox;
import cn.edu.cjw.APMFP.ui.component.PlaceholderTextField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @package cn.edu.cjw.APMFP.ui.panel
 * @className PersonPanel
 * @note 审核任务
 * @author wind
 * @date 2022/3/12 13:12
 */
public class CheckPanel extends JPanel {

    private CenterPanel mainPanel;

    private TaskService taskService = new TaskServiceImpl();

    private WsService wsService = new WsServiceImpl();

    private ProductService productService = new ProductServiceImpl();

    private List<ProductionTask> taskList = new ArrayList<>();
    private List<Product> productList = new ArrayList<>();
    private Map<Object, Object> productMap = new HashMap<>();
    private List<WorkStep> stepList = new ArrayList<>();
    private List<BOMTable> bomList = new ArrayList<>();

    private int count = 1;
    private List<JComboBox<String>> filterList = new ArrayList<>();
    private List<PlaceholderTextField> countList = new ArrayList<>();

    public CheckPanel(CenterPanel mainPanel) {
        this.mainPanel = mainPanel;
        init();
    }

    public void init(){
        try {
            taskList.addAll(taskService.searchTaskByExamine());
            stepList.addAll(wsService.showAll());
            productList.addAll(productService.showAll());
            productList.forEach(p -> {
                productMap.put(p.getBId(), p.getBName());
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.setLayout(new BorderLayout(10, 10));
        JPanel taskJPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
        taskJPanel.setBorder(BorderFactory.createTitledBorder("未审核任务"));
        taskJPanel.setPreferredSize(new Dimension(560, 100));
        this.add(taskJPanel, BorderLayout.NORTH);
        JComboBox<String> taskBox = new JComboBox<>();
        taskList.forEach(t -> taskBox.addItem(t.getTaskId()));
        taskBox.setPreferredSize(new Dimension(300, 30));
        taskJPanel.add(taskBox);

        JScrollPane scrollPane = new JScrollPane();
        GridBagLayout gridBagLayout = new GridBagLayout();
        JPanel centerJPanel = new JPanel(gridBagLayout);
        centerJPanel.setBorder(BorderFactory.createTitledBorder("产品"));
//        centerJPanel.setPreferredSize(new Dimension(560, 220));
        initGridPanel(centerJPanel);
        scrollPane.setViewportView(centerJPanel);
        this.add(scrollPane, BorderLayout.CENTER);


        JPanel footerJPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
        this.add(footerJPanel, BorderLayout.SOUTH);
        JButton button = new JButton("审核");
        button.setPreferredSize(new Dimension(100, 30));
        footerJPanel.add(button);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int index = taskBox.getSelectedIndex();
                if(index == -1){
                    JOptionPane.showMessageDialog(null, "请选择任务");
                    return;
                }

                HashMap<String, Long> productMap = new HashMap<>();
                for(int i = 0; i < filterList.size(); i++){
                    int i1 = filterList.get(i).getSelectedIndex();
                    if(i1 == -1){
                        JOptionPane.showMessageDialog(null, "请选择产品");
                        return;
                    }
                    String id = productList.get(i1).getBId();
                    String countStr = countList.get(i).getText();
                    if(!StringUtil.isInt(countStr)){
                        JOptionPane.showMessageDialog(null, "请输入合适数量");
                        return;
                    }
                    productMap.put(id, Long.valueOf(countStr));
                }
                try {
                    taskService.ExamineTask(taskList.get(index), productMap);
                    JOptionPane.showMessageDialog(null, "审核成功");
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "审核失败");
                }
            }
        });
    }

    public void initGridPanel(JPanel centerJPanel){
        centerJPanel.removeAll();
        filterList.clear();
        countList.clear();
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(10, 10, 10, 10);
        constraints.ipady = 10;
        for(int i = 0; i < count; i++){
            final int index = i;
//            JFilterComboBox pField = new JFilterComboBox(productList.stream().map(Product::getBName).toArray());
            JComboBox<String> pField = new JComboBox(productList.stream().map(Product::getBName).toArray());
            pField.setPreferredSize(new Dimension(200, 30));
            PlaceholderTextField cFiled = new PlaceholderTextField("", 27);
            cFiled.setPlaceholder("数量");
            JButton addBtn = new JButton(i == 0 ? "+" : "-");
            constraints.gridx = 0;
            constraints.gridy = i;
            centerJPanel.add(pField, constraints);
            constraints.gridx = 1;
            centerJPanel.add(cFiled, constraints);
            constraints.gridx = 2;
            centerJPanel.add(addBtn, constraints);
            filterList.add(pField);
            countList.add(cFiled);

            addBtn.addActionListener(e -> {
                if(index == 0){
                    count++;
                    initGridPanel(centerJPanel);
                }else{
                    count--;
                    initGridPanel(centerJPanel);
                }
            });
        }
        centerJPanel.updateUI();
        centerJPanel.repaint();
    }
}
